/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatStation;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.model.IShareArea;
import com.nari.baseinfo.service.IBaseStatisService;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICityService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatStationService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.utils.BaseStatisConstants;
import com.nari.baseinfo.utils.statisentity.IGenericStatisEntity;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class CommunityAction extends JSONActionSupport {
	private static final long serialVersionUID = 7982846388459831928L;
	private ICommunityService iCommunityService;
	private ICityService iCityService;
	private IProjectService iProjectService;
	private IHeatStationService iHeatStationService;
	private IBuildingService iBuildingService;
	private IBaseStatisService iBaseStatisService;
	// 小区信息查询
	private String communityName;
	private String districtCityID;
	private String capitalCityID;
	private String provinceCityID;
	private List<ICity> provinceCityList;
	private List<ICity> capitalCityList;
	private List<ICity> districtCityList;
	private String projectUID;
	private List<IProject> projectList;
	private List<IShareArea> shareAreaList;
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<ICommunity> communityPage;
	private Map<String, String> pageCityMap; // 所属城市id-cityName映射表
	private Map<String, String> pageProjectMap; // 所属项目id-projectName映射表
	// 小区信息管理
	private String communityUID;
	private ICommunity community;
	private List<IHeatStation> stationList;
	// 小区列表查询
	private List<ICommunity> communityList;
	//小区资料信息
	private IGenericStatisEntity statisEntity;
	private IProject project;
	private List<IBuilding> buildingList;

	/**
	 * 分页查询小区信息
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryCommunityPage() throws Exception {
		// 机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if (provinceCityID != null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if (capitalCityID != null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		// 项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (projectList == null) {
			projectList = new ArrayList<IProject>();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 分页结果
		this.communityPage = this.iCommunityService.queryCommunityWithPageCond(communityName, provinceCityID, capitalCityID, districtCityID, projectUID, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.communityPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.communityPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		// 查询小区信息对应的行政机构映射表
		this.pageCityMap = this.iCommunityService.queryReferCitysByCommunityList(communityPage.getList());
		// 查询小区信息对应的项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : this.projectList) {
			pageProjectMap.put(project.getId(), project.getProjectName());
		}
		return "communityList";
	}

	/**
	 * 小区信息管理
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryCommunityForManage() throws Exception {
		// 小区信息
		if (this.communityUID != null && !"".equals(communityUID.trim())) {
			this.community = this.iCommunityService.queryEntityById(this.communityUID.trim());
			this.provinceCityID = this.community.getProvinceCityID();
			this.capitalCityID = this.community.getCapitalCityID();
			this.districtCityID = this.community.getDistrictCityID();
		}
		// 机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if (provinceCityID != null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if (capitalCityID != null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		// 所属项目列表及换热站设置
		if ((provinceCityID != null && !"".equals(provinceCityID))
				|| (capitalCityID != null && !"".equals(capitalCityID))) {
			this.projectList = this.iProjectService.queryProjectsByBelongCity(provinceCityID, capitalCityID);
			// 换热站设置
			this.stationList = this.iHeatStationService.queryStationByBelongCity(districtCityID, capitalCityID, provinceCityID);
		} else {
			this.projectList = this.iProjectService.queryAllProjectInOrder();
		}
		if (projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		if (stationList == null) {
			stationList = new ArrayList<IHeatStation>();
		}
		return "communityManage";
	}

	/**
	 * 新增或修改小区信息
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String saveOrUpdateCommunity() throws Exception {
		if (this.community.getId() != null && !"".equals(this.community.getId())) {
			this.iCommunityService.updateEntity(community, community.getId());
		} else {
			this.iCommunityService.saveEntity(community);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}

	/**
	 * 通过唯一标识删除小区信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String deleteCommunityByUID() throws Exception {
		this.rmsg = this.iCommunityService.deleteCascadeByCommunityUID(communityUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 根据所属信息（所属城市、所属项目）查询对应的小区列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public String queryCommunitysByBelongs() throws Exception {
		this.communityList = this.iCommunityService.queryCommunitysByBelongings(provinceCityID, 
				capitalCityID, districtCityID, projectUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.communityList));
		return Action.SUCCESS;
	}
	/**
	 * 查看小区详细信息
	 * @return
	 * @throws Exception
	 * @author yw
	 * @date 2013-9-12
	 */
	@SuppressWarnings("unchecked")
	public String showCommunityDetail() throws Exception {
		// 小区信息
		if (this.communityUID != null && !"".equals(communityUID.trim())) {
			this.community = this.iCommunityService.queryEntityById(this.communityUID.trim());
			// 查询小区信息对应的行政机构映射表
			this.pageCityMap = this.iCommunityService.queryReferCitysByCommunityList(Arrays.asList(new ICommunity[]{community}));
			//所属项目
			this.project = iProjectService.queryEntityById(community.getProjectID());
			//统计信息
			this.statisEntity = iBaseStatisService.queryBaseStatisEntity(BaseStatisConstants.statisType_communityDetail, communityUID);
			//所属楼栋
			this.buildingList = iBuildingService.queryBuildingByBelongings(null, communityUID);
		}
		return "communityDetail";
	}
	
	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService
	 *            the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iCityService
	 */
	@JSON(serialize = false)
	public ICityService getiCityService() {
		return iCityService;
	}

	/**
	 * @param iCityService
	 *            the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService
	 *            the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iHeatStationService
	 */
	@JSON(serialize = false)
	public IHeatStationService getiHeatStationService() {
		return iHeatStationService;
	}

	/**
	 * @param iHeatStationService
	 *            the iHeatStationService to set
	 */
	public void setiHeatStationService(IHeatStationService iHeatStationService) {
		this.iHeatStationService = iHeatStationService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize = false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iBaseStatisService
	 */
	@JSON(serialize = false)
	public IBaseStatisService getiBaseStatisService() {
		return iBaseStatisService;
	}

	/**
	 * @param iBaseStatisService the iBaseStatisService to set
	 */
	public void setiBaseStatisService(IBaseStatisService iBaseStatisService) {
		this.iBaseStatisService = iBaseStatisService;
	}

	/**
	 * @return the communityName
	 */
	public String getCommunityName() {
		return communityName;
	}

	/**
	 * @param communityName
	 *            the communityName to set
	 */
	public void setCommunityName(String communityName) {
		this.communityName = communityName;
	}

	/**
	 * @return the districtCityID
	 */
	public String getDistrictCityID() {
		return districtCityID;
	}

	/**
	 * @param districtCityID
	 *            the districtCityID to set
	 */
	public void setDistrictCityID(String districtCityID) {
		this.districtCityID = districtCityID;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID
	 *            the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID
	 *            the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the provinceCityList
	 */
	public List<ICity> getProvinceCityList() {
		return provinceCityList;
	}

	/**
	 * @param provinceCityList
	 *            the provinceCityList to set
	 */
	public void setProvinceCityList(List<ICity> provinceCityList) {
		this.provinceCityList = provinceCityList;
	}

	/**
	 * @return the capitalCityList
	 */
	public List<ICity> getCapitalCityList() {
		return capitalCityList;
	}

	/**
	 * @param capitalCityList
	 *            the capitalCityList to set
	 */
	public void setCapitalCityList(List<ICity> capitalCityList) {
		this.capitalCityList = capitalCityList;
	}

	/**
	 * @return the districtCityList
	 */
	public List<ICity> getDistrictCityList() {
		return districtCityList;
	}

	/**
	 * @param districtCityList
	 *            the districtCityList to set
	 */
	public void setDistrictCityList(List<ICity> districtCityList) {
		this.districtCityList = districtCityList;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID
	 *            the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList
	 *            the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex
	 *            the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond
	 *            the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the communityPage
	 */
	public ResultWithPageCond<ICommunity> getCommunityPage() {
		return communityPage;
	}

	/**
	 * @param communityPage
	 *            the communityPage to set
	 */
	public void setCommunityPage(ResultWithPageCond<ICommunity> communityPage) {
		this.communityPage = communityPage;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap
	 *            the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap
	 *            the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID
	 *            the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community
	 *            the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the stationList
	 */
	public List<IHeatStation> getStationList() {
		return stationList;
	}

	/**
	 * @param stationList
	 *            the stationList to set
	 */
	public void setStationList(List<IHeatStation> stationList) {
		this.stationList = stationList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList
	 *            the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	public List<IShareArea> getShareAreaList() {
		return shareAreaList;
	}

	public void setShareAreaList(List<IShareArea> shareAreaList) {
		this.shareAreaList = shareAreaList;
	}
	
	/**
	 * @return the statisEntity
	 */
	public IGenericStatisEntity getStatisEntity() {
		return statisEntity;
	}

	/**
	 * @param statisEntity the statisEntity to set
	 */
	public void setStatisEntity(IGenericStatisEntity statisEntity) {
		this.statisEntity = statisEntity;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

}
